package qnb.controller;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;

import qnb._common.easyui.EasyuiGridData;
import qnb._common.easyui.EasyuiPageVO;
import qnb._common.vo.MessageVOFactory;
import qnb._config.Aliyun_sms;
import qnb.entity.Smsinfo;
import qnb.entity.Teacher;
import qnb.service.TeacherServiceDao;
import qnb.service.impl.TeacherServiceDaoImp;
import weixinkeji.vip.expand.poi.JWEOfficeWebTool;
import weixinkeji.vip.jweb.power.ann.JWPIdentifiter;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
	@Autowired
	private TeacherServiceDao imp;
	// 跳转
	/**
	 * 跳转到教师添加页面
	 * @return
	 */
	@JWPIdentifiter("teacher:add")
	@RequestMapping("/forward/addTeacher")
	public ModelAndView forwardAdd(ModelAndView modelAndView) {
		modelAndView.setViewName("teacher/addTeacher");
		return modelAndView;
	}
	/**
	 * 跳转到教师列表页面
	 * @return
	 */
	@JWPIdentifiter("teacher:select")
	@RequestMapping("/forward/list")
	public ModelAndView forwardList(ModelAndView modelAndView) {
		modelAndView.setViewName("teacher/TeacherList");
		return modelAndView;
	}
	//处理请求

	/**
	 * 处理添加教师信息
	 * @param tea
	 * @return
	 * @throws ClientException
	 */
	@JWPIdentifiter("teacher:add")
	@RequestMapping("/addTea")
	@ResponseBody
	public String addTeacher(Teacher tea) throws ClientException {
		Smsinfo info = new Smsinfo();
		info.setName(tea.getTeacherName());
		info.setAccount(tea.getTeacherAccount());
		info.setPassword(tea.getTeacherPassword());
		if(imp.checkId(tea.getTeacherId())>0) {
			return "{\"msg\":\"添加失败:教师编号已经存在,请重新输入!\"}";
		}
		if(imp.checkAccount(tea.getTeacherAccount())>0) {
			return "{\"msg\":\"添加失败:教师账号已经存在,请重新输入!\"}";
		}
		int i=imp.addTeacher(tea);
		if(i>0) {
		System.out.println("这里了");
		Aliyun_sms.sms(tea.getTeacherPhone(), JSON.toJSONString(info));
		}
		return MessageVOFactory.getAddMessage(i);
	}


	/**
	 * 处理教师页面以及模糊查找教师信息
	 * @param vo
	 * @param req
	 * @return
	 */
	@JWPIdentifiter("teacher:select")
	@RequestMapping("/TeacherList")
	@ResponseBody
	public Object teacherList(EasyuiPageVO vo,HttpServletRequest req) {
		String findName=req.getParameter("findName");
		String style=req.getParameter("style");
		if (style != null && style.trim().length() == 0) {
			style = null;
		}
		List<Teacher> teachers = imp.selectForPage(style, findName, vo.getStart(), vo.getRows());
		teachers = teachers.stream().filter(t->!t.getTeacherAccount().equals("admin")).collect(Collectors.toList());
		return EasyuiGridData.getEasyuiGridData(imp.selectCount(),teachers);
	}
	/**
	 * 处理修改教师信息的请求
	 * @param teacher
	 * @return
	 */
	@JWPIdentifiter("teacher:update")
	@RequestMapping("/UpdateTeacher")
	@ResponseBody
	public String updateTeacher(Teacher teacher) {
		System.out.println(teacher.getTeacherBirthday());
		return MessageVOFactory.getUpdateMessage(imp.updateTeacher(teacher));
	}
	/**
	 * 根据ID请求教师信息，填充到表单
	 * @param
	 * @return
	 */
	@RequestMapping("/dataForm")
	@ResponseBody
	public Teacher selectOne(String tid) {
		return imp.selectOne(tid);
	}
	/**
	 * 根据ID进行教师信息的批量删除操作
	 * @param list
	 * @return
	 */
	@JWPIdentifiter("teacher:dell")
	@RequestMapping("/deleteTeacher")
	@ResponseBody
	public String deleteList(String[] list) {
		return MessageVOFactory.getDellMessage(imp.deleteTeacher(list));
	}

	/**
	 * 处理Excel下载的请求
	 * @param
	 * @param res
	 * @throws ServletException
	 * @throws IOException
	 */
	@JWPIdentifiter("teacher:down")
	@RequestMapping("/download")
	public void Dowanload(HttpServletResponse res) throws ServletException, IOException {
		JWEOfficeWebTool.downloadExcelFile( res, imp.selectAll(), "教师信息.xls", "教师信息", true);

	}
	/**
	 * 处理上传EXcel的请求
	 * @throws IOException
	 * @throws ServletException
	 */
	@JWPIdentifiter("teacher:upload")
	@RequestMapping("/upload")
	@ResponseBody
	public String upData(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		//根据上传的文件获得相应的对象集合
		List<Teacher> list=JWEOfficeWebTool.getUploadExcelFile(req, "file", Teacher.class);
		List<String> id = new ArrayList<String>();//定义一个ID集合
		List<String> account=new ArrayList<String>(); //定义一个账号集合
		//如果不为null则有数据
		if(list!=null) {
			//将账号编号，填充到集合当中
			for(Teacher tea:list) {
				id.add(tea.getTeacherId());
			account.add(tea.getTeacherAccount());
			}
			//如果为null说明账号或ID不重复
			if(imp.checkExcel(id).size()>0) {
				System.out.println(imp.checkExcel(id).size());
				return "{\"msg\":\"教师编号已存在,请检查后上传！\"}";
			}
			if(imp.checkExcel2(account).size()>0) {
				return "{\"msg\":\"教师账号已存在,请检查后上传！\"}";
			}
			return MessageVOFactory.getAddMessage(imp.addExcelData(list));

		}
		return "{\"msg\":\"选择的文件类型错误或文件内容为空！\"}";
	}
}
